Backup n8n Workflows to Bitbucket
工作流概述
这是一个包含9个节点的复杂工作流,主要用于自动化处理各种任务。
工作流源代码
{
"id": "23GPrqZjHnIVvTEa",
"meta": {
"instanceId": "[instance id auto generated]",
"templateCredsSetupCompleted": true
},
"name": "Backup n8n Workflows to Bitbucket",
"tags": [],
"nodes": [
{
"id": "b3363b9d-ea6e-47b7-99f9-f48a21805886",
"name": "Calculate Wait Time",
"type": "n8n-nodes-base.code",
"position": [
1400,
-260
],
"parameters": {
"jsCode": "// Get all input items and ensure we have data
if ($input.all().length === 0 || !$input.all()[0].headers) {
// If no headers available, return default wait time
return { waitTime: 1 };
}
// Check rate limit headers from previous request
const headers = $input.all()[0].headers;
let waitTime = 1; // Default 1 second
// Check if we have rate limit information (safely)
const remaining = parseInt(headers['x-ratelimit-remaining']) || null;
const reset = parseInt(headers['x-ratelimit-reset']) || null;
// Only adjust wait time if we have valid rate limit info
if (remaining !== null && reset !== null) {
// If we're running low on requests, calculate a longer wait time
if (remaining < 100) {
// Calculate seconds until reset
const now = Math.floor(Date.now() / 1000);
const timeUntilReset = reset - now;
// Spread remaining requests over time until reset
// Add 10% buffer to be safe
waitTime = Math.ceil((timeUntilReset / remaining) * 1.1);
} else if (remaining < 500) {
// Start slowing down earlier
waitTime = 2;
}
}
// Cap maximum wait time at 30 seconds
waitTime = Math.min(waitTime, 30);
return { waitTime };"
},
"typeVersion": 2
},
{
"id": "3cbc2287-b36f-4839-87b7-be4a7eadcf79",
"name": "Run Daily at 2 AM",
"type": "n8n-nodes-base.scheduleTrigger",
"position": [
-120,
-20
],
"parameters": {
"rule": {
"interval": [
{
"triggerAtHour": 2
}
]
}
},
"typeVersion": 1.2
},
{
"id": "09b396aa-61e8-4631-8aae-7126fbd609e6",
"name": "Get All Workflows",
"type": "n8n-nodes-base.n8n",
"position": [
320,
-20
],
"parameters": {
"filters": {},
"requestOptions": {}
},
"credentials": {
"n8nApi": {
"id": "[n8n-api-credential-id]",
"name": "n8n Development Environment"
}
},
"typeVersion": 1
},
{
"id": "c46b50cd-432f-4714-ac68-b6f92663b592",
"name": "Loop Workflows",
"type": "n8n-nodes-base.splitInBatches",
"position": [
540,
-20
],
"parameters": {
"options": {}
},
"typeVersion": 3
},
{
"id": "2a27e85d-51c0-4f45-a7d6-6422fc8a439b",
"name": "Get Existing Worfklow from Bitbucket",
"type": "n8n-nodes-base.httpRequest",
"onError": "continueRegularOutput",
"position": [
780,
-20
],
"parameters": {
"url": "=https://api.bitbucket.org/2.0/repositories/{{ $('Set Bitbucket Workspace & Repository').item.json.WorkspaceSlug }}/{{ $('Set Bitbucket Workspace & Repository').item.json.RepositorySlug }}/src/main/{{ $json.name.replace(/[^a-zA-Z0-9]/g, '-').toLowerCase() }}",
"options": {
"response": {
"response": {
"fullResponse": true
}
},
"allowUnauthorizedCerts": true
},
"authentication": "genericCredentialType",
"genericAuthType": "httpBasicAuth"
},
"credentials": {
"httpBasicAuth": {
"id": "[bitbucket-credential-id]",
"name": "Bitbucket"
}
},
"retryOnFail": false,
"typeVersion": 4.2,
"alwaysOutputData": false
},
{
"id": "eeb52f03-dd60-46ae-ad86-1cabf7f6c20f",
"name": "New or Changed?",
"type": "n8n-nodes-base.if",
"position": [
980,
-20
],
"parameters": {
"options": {
"ignoreCase": true
},
"conditions": {
"options": {
"version": 2,
"leftValue": "",
"caseSensitive": false,
"typeValidation": "strict"
},
"combinator": "or",
"conditions": [
{
"id": "2d5da90e-0f1d-436b-84d4-d82deaaa4b58",
"operator": {
"type": "number",
"operation": "equals"
},
"leftValue": "={{ $json.error.status }}",
"rightValue": 404
},
{
"id": "b7b9a48d-8954-4cc4-bf7a-ab30439ad930",
"operator": {
"type": "string",
"operation": "notEquals"
},
"leftValue": "={{ $('Get Existing Worfklow from Bitbucket').item.json.data }}",
"rightValue": "={{ JSON.stringify($('Loop Workflows').item.json, null, 2) }}"
}
]
}
},
"typeVersion": 2.2
},
{
"id": "04400827-d331-4ee2-8a67-1238ea2dc969",
"name": "Upload Workflow to Bitbucket",
"type": "n8n-nodes-base.httpRequest",
"position": [
1200,
-260
],
"parameters": {
"url": "=https://api.bitbucket.org/2.0/repositories/{{ $('Set Bitbucket Workspace & Repository').item.json.WorkspaceSlug }}/{{ $('Set Bitbucket Workspace & Repository').item.json.RepositorySlug }}/src",
"method": "POST",
"options": {
"redirect": {
"redirect": {
"maxRedirects": 5
}
},
"response": {
"response": {
"fullResponse": true
}
}
},
"sendBody": true,
"sendHeaders": true,
"authentication": "genericCredentialType",
"bodyParameters": {
"parameters": [
{
"name": "message",
"value": "={{ $('Loop Workflows').item.json.name + ' [' + $now.format('yyyy-MM-dd HH:mm:ss') +']' }}"
},
{
"name": "={{ $('Loop Workflows').item.json.name.replace(/[^a-zA-Z0-9]/g, '-').toLowerCase() }}",
"value": "={{ JSON.stringify($('Loop Workflows').item.json, null, 2) }}"
}
]
},
"genericAuthType": "httpBasicAuth",
"headerParameters": {
"parameters": [
{
"name": "Content-Type",
"value": "application/x-www-form-urlencoded"
}
]
}
},
"credentials": {
"httpBasicAuth": {
"id": "[bitbucket-credential-id]",
"name": "Bitbucket"
}
},
"typeVersion": 4.2
},
{
"id": "5f198366-3bcf-4a96-ae60-da7cc9403a6f",
"name": "Wait to Avoid Rate Limiting",
"type": "n8n-nodes-base.wait",
"position": [
1620,
-20
],
"webhookId": "793d7525-d166-4487-a71f-d48da7c66662",
"parameters": {
"amount": "={{ $json.waitTime || 1 }}"
},
"typeVersion": 1.1
},
{
"id": "adc37b33-c5af-4a44-ba87-9806efe25603",
"name": "Set Bitbucket Workspace & Repository",
"type": "n8n-nodes-base.set",
"position": [
100,
-20
],
"parameters": {
"options": {},
"assignments": {
"assignments": [
{
"id": "37f2ddba-188d-4bc1-98b3-5c5fa31d2d62",
"name": "WorkspaceSlug",
"type": "string",
"value": "[workspace-slug]"
},
{
"id": "303f25f0-bba8-4977-8f4f-33961e2e7e8c",
"name": "RepositorySlug",
"type": "string",
"value": "[repository-slug]"
}
]
}
},
"typeVersion": 3.4
}
],
"active": true,
"pinData": {},
"settings": {
"executionOrder": "v1"
},
"versionId": "f21887f2-e885-42c6-a934-4f7617e267dd",
"connections": {
"Loop Workflows": {
"main": [
[],
[
{
"node": "Get Existing Worfklow from Bitbucket",
"type": "main",
"index": 0
}
]
]
},
"New or Changed?": {
"main": [
[
{
"node": "Upload Workflow to Bitbucket",
"type": "main",
"index": 0
}
],
[
{
"node": "Wait to Avoid Rate Limiting",
"type": "main",
"index": 0
}
]
]
},
"Get All Workflows": {
"main": [
[
{
"node": "Loop Workflows",
"type": "main",
"index": 0
}
]
]
},
"Run Daily at 2 AM": {
"main": [
[
{
"node": "Set Bitbucket Workspace & Repository",
"type": "main",
"index": 0
}
]
]
},
"Calculate Wait Time": {
"main": [
[
{
"node": "Wait to Avoid Rate Limiting",
"type": "main",
"index": 0
}
]
]
},
"Wait to Avoid Rate Limiting": {
"main": [
[
{
"node": "Loop Workflows",
"type": "main",
"index": 0
}
]
]
},
"Upload Workflow to Bitbucket": {
"main": [
[
{
"node": "Calculate Wait Time",
"type": "main",
"index": 0
}
]
]
},
"Get Existing Worfklow from Bitbucket": {
"main": [
[
{
"node": "New or Changed?",
"type": "main",
"index": 0
}
]
]
},
"Set Bitbucket Workspace & Repository": {
"main": [
[
{
"node": "Get All Workflows",
"type": "main",
"index": 0
}
]
]
}
}
}
功能特点
- 自动检测新邮件
- AI智能内容分析
- 自定义分类规则
- 批量处理能力
- 详细的处理日志
技术分析
节点类型及作用
- Code
- Scheduletrigger
- N8N
- Splitinbatches
- Httprequest
复杂度评估
配置难度:
维护难度:
扩展性:
实施指南
前置条件
- 有效的Gmail账户
- n8n平台访问权限
- Google API凭证
- AI分类服务订阅
配置步骤
- 在n8n中导入工作流JSON文件
- 配置Gmail节点的认证信息
- 设置AI分类器的API密钥
- 自定义分类规则和标签映射
- 测试工作流执行
- 配置定时触发器(可选)
关键参数
| 参数名称 | 默认值 | 说明 |
|---|---|---|
| maxEmails | 50 | 单次处理的最大邮件数量 |
| confidenceThreshold | 0.8 | 分类置信度阈值 |
| autoLabel | true | 是否自动添加标签 |
最佳实践
优化建议
- 定期更新AI分类模型以提高准确性
- 根据邮件量调整处理批次大小
- 设置合理的分类置信度阈值
- 定期清理过期的分类规则
安全注意事项
- 妥善保管API密钥和认证信息
- 限制工作流的访问权限
- 定期审查处理日志
- 启用双因素认证保护Gmail账户
性能优化
- 使用增量处理减少重复工作
- 缓存频繁访问的数据
- 并行处理多个邮件分类任务
- 监控系统资源使用情况
故障排除
常见问题
邮件未被正确分类
检查AI分类器的置信度阈值设置,适当降低阈值或更新训练数据。
Gmail认证失败
确认Google API凭证有效且具有正确的权限范围,重新进行OAuth授权。
调试技巧
- 启用详细日志记录查看每个步骤的执行情况
- 使用测试邮件验证分类逻辑
- 检查网络连接和API服务状态
- 逐步执行工作流定位问题节点
错误处理
工作流包含以下错误处理机制:
- 网络超时自动重试(最多3次)
- API错误记录和告警
- 处理失败邮件的隔离机制
- 异常情况下的回滚操作